package com.alameda.config.quartz;

import com.alameda.commonpent.job.JobCommonpent;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * quartz 配置类
 *
 * @author Lv
 * @date 2024/7/23
 */
@Configuration
public class QuartzConfig {
    /**
     * 定义定时任务对象
     *
     * @return
     */
    @Bean
    public JobDetail myJobDetail() {
        return JobBuilder.newJob(JobCommonpent.class)
                .withIdentity("job", "group1")
                .storeDurably()
                .build();
    }

    /**
     * 触发器对象
     *
     * @param job 定时任务对象
     * @return
     */
    @Bean
    public Trigger myTrigger(JobDetail job) {
        // 使用cron表达式触发任务
        return TriggerBuilder.newTrigger()
                .forJob(job)
                .withIdentity("trigger", "group1")
                // 每两分钟执行一次
                .withSchedule(CronScheduleBuilder.cronSchedule("0 0/2 * * * ?"))
                .build();
    }
}